﻿using System;
using System.Windows.Forms;
using Pdftools.Pdf;
using Pdftools.PdfOptimize;

namespace EvTest.PdfTools
{
    public partial class OP : Form
    {
        public OP()
        {
            InitializeComponent();
        }

        private void OP_Load(object sender, EventArgs e)
        {

        }

        private void ButtonOptimize_Click(object sender, EventArgs e)
        {
            ButtonOptimize.Enabled = false;
            ButtonOptimize.Text = "转换中···";

            try
            {

                txtLog.Text = String.Empty;
                if (txtOutput.Text.Equals(String.Empty))
                {
                    txtLog.Text = ""; //Resources.strOutputDirErr;
                    return;
                }

                using (Optimizer opt = new Optimizer())
                {
                    if (!opt.Open(txtInput.Text, txtPW.Text))
                    {
                        txtLog.Text = "错误异常：" + opt.ErrorCode.ToString() + String.Format(" ({0})", opt.ErrorMessage);
                        return;
                    }

                    opt.ColorResolutionDPI = 72; // 上限是150，值越低则色彩越寡淡，文件尺寸也会相应的缩小
                    opt.ImageQuality = (Int32)nt.Value;//1-100，输出图片质量，数值越高，图像精度越高
                    opt.AutoLinearize = true;// 针对Web浏览的效率，线性化PDF
                    opt.ClipImages = cbClip.Checked;// 图片是否支持被复制
                    // 更多可优化属性选项见Optimizer类中描述

                    if (rbRGB.Checked)
                    {

                        opt.ColorConversion = PDFColorConversion.eConvRGB;// 指定输出的色彩格式
                    }
                    if (rbCMKY.Checked)
                    {
                        opt.ColorConversion = PDFColorConversion.eConvCMYK;// 指定输出的色彩格式
                    }
                    if (rbGray.Checked)
                    {
                        opt.ColorConversion = PDFColorConversion.eConvGray;// 指定输出的色彩格式
                    }

                    opt.MrcLayerQuality = 50;// 1-100,默认50
                    opt.RemoveImages = rbImg.Checked;// 是否移除图像，
                    opt.RemoveStandardFonts = rbFont.Checked;// 移除字体


                    String strFilename = null;
                    if (rbProfilePrint.Checked)
                    {
                        // 针对印刷优化
                        opt.Profile = PDFOptimizationProfile.eOptimizationProfilePrint;
                        strFilename = "印刷优化.pdf";
                    }
                    else if (rbWeb.Checked)
                    {
                        // web线性优化
                        opt.Profile = PDFOptimizationProfile.eOptimizationProfileWeb;
                        strFilename = "Web优化.pdf";
                    }
                    if (strFilename != null)
                    {
                        strFilename = txtOutput.Text + "/" + strFilename;
                        if (opt.SaveAs(strFilename, String.Empty, String.Empty, PDFPermission.ePermNoEncryption))
                            txtLog.Text = "文件保存成功：" + strFilename;
                        else
                            txtLog.Text = "文件保存异常：" + opt.ErrorCode.ToString() + String.Format(" ({0})", opt.ErrorMessage);
                    }
                    opt.Close();

                }
            }
            catch (Exception ex)
            {
                txtLog.Text = ex.Message;
            }
            finally
            {
                ButtonOptimize.Enabled = true;
                ButtonOptimize.Text = "优化";
            }
        }

        private void OpenPDF_Click(object sender, EventArgs e)
        {

            try
            {
                openFileDialog1.FileName = txtInput.Text;
                openFileDialog1.ShowDialog();
                txtInput.Text = openFileDialog1.FileName;
            }
            catch (InvalidOperationException)
            {
                txtInput.Text = String.Empty;
                return;
            }
        }

        private void SetOutputDir_Click(object sender, EventArgs e)
        {

            folderBrowserDialog1.SelectedPath = txtOutput.Text;
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                txtOutput.Text = folderBrowserDialog1.SelectedPath;
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            try
            {
                if (!Optimizer.LicenseIsValid)
                    MessageBox.Show(this, "No valid license found.", "Pdf Optimization Sample", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (TypeInitializationException)
            {
                // 原因：解决方案CPU方案使用Any CPU，那么我们引入的api.dll需要确认X86还是X64
                MessageBox.Show(this, "A type initialization exception was thrown.\n\n" +
                                      " The usual reasons for this are:\n" +
                                      "  - The native DLL (PdfOptimizeAPI.dll) could not be found.\n" +
                                      "  - The native DLL has the wrong format (32-Bit vs. 64-Bit)\n\n" +
                                      "Click 'OK' to debug or 'Help' to get more information.",
                                      "Pdf Optimization Sample", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0,
                                      "http://www.pdf-tools.com/pdf/Support/FAQ/Article.aspx?name=Exception-type-initializer");
                throw;
            }
        }

        private void cbPw_CheckedChanged(object sender, EventArgs e)
        {
            if (cbPw.Checked)
            {
                txtPW.Enabled = true;
            }
            else
            {
                txtPW.Enabled = false;
            }
        }

        private void hScrollBar1_ValueChanged(object sender, EventArgs e)
        {
            nt.Value = hScrollBar1.Value;
        }
    }
}
